package cn.lingyangwl.agile.infra.gen.controller;

import cn.lingyangwl.agile.infra.datamanage.mode.entity.*;
import cn.lingyangwl.agile.infra.datamanage.service.*;
import cn.lingyangwl.agile.infra.gen.model.entity.*;
import cn.lingyangwl.agile.infra.gen.model.rqrs.*;
import cn.lingyangwl.agile.infra.gen.service.*;
import cn.lingyangwl.agile.ram.*;
import cn.lingyangwl.framework.core.response.*;
import com.alibaba.fastjson2.JSON;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author shenguangyang
 */
@Api(tags = "低代码-生成历史")
@RestController
@RequestMapping("genHistory")
@RequiredArgsConstructor
public class GenerateHistoryController {
    private final GenerateHistoryService generateHistoryService;
    private final DatasourceService datasourceService;
    private final IGenTableService genTableService;
    private final TemplateConfigService templateConfigService;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @RequestMapping("listHistory")
    @RamResource(action = "lcp:history:query", name = "低代码-查询生成代码历史", type = ActionType.READ)
    public Resp<List<GenerateHistoryResp>> listAll() {
        List<GenerateHistory> generateHistories = generateHistoryService.listAll();
        List<GenerateHistoryResp> generateHistoryRespList = generateHistories.stream()
                .map(generateHistory -> {
                    GenerateHistoryResp generateHistoryResp = new GenerateHistoryResp();
                    GeneratorReq generatorReq = JSON.parseObject(generateHistory.getConfigContent(), GeneratorReq.class);
                    GenTableDto genTableDto = genTableService.getGenTableRespByTableId(generatorReq.getId());
                    if (genTableDto == null) {
                        return null;
                    }
                    DatasourceConfig datasourceConfig = datasourceService.getById(genTableDto.getDatasourceId());
                    if (Objects.isNull(datasourceConfig)) {
                        return null;
                    }
                    generateHistoryResp.setCreatedAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(generateHistory.getCreatedAt()));
                    generateHistoryResp.setConfigContent(generatorReq);
                    generateHistoryResp.setDatasource(datasourceConfig.getName());
                    return generateHistoryResp;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        return Resp.ok(generateHistoryRespList);
    }

    private List<String> listTemplateNames(List<Long> idList) {
        return templateConfigService.listByIds(idList)
                .stream()
                .map(TemplateConfig::getName)
                .collect(Collectors.toList());
    }

}